//
//  RootViewController.m
//  UIDeviceInfo
//
//  Created by Naoki TSUTSUI on 10/02/06.
//  Copyright iphoneworld.jp 2010. All rights reserved.
//

#import "RootViewController.h"


@interface RootViewController (Private)
- (void)setDeviceInfoArray;
- (void)alertWithMessaeg:(NSString *)message;
@end

@implementation RootViewController
@synthesize deviceInfoArray;

- (void)dealloc {
  [deviceInfoArray release];
  [super dealloc];
}

- (void)setDeviceInfoArray {
  UIDevice *currentDevice = [UIDevice currentDevice];
  
  // 取得したデバイス情報をログ出力
  NSLog(@"model:%@", [currentDevice model]);
  NSLog(@"localizedModel:%@", [currentDevice localizedModel]);
  NSLog(@"name:%@", [currentDevice name]);
  NSLog(@"uniqueIdentifier:%@", [currentDevice uniqueIdentifier]);
  NSLog(@"systemName:%@", [currentDevice systemName]);
  NSLog(@"systemVersion:%@", [currentDevice systemVersion]);
  
  // UITableViewに表示するためのデータソースにデバイス情報をセット
  deviceInfoArray = [[NSMutableArray alloc] init];
  [deviceInfoArray addObject:[NSString stringWithFormat:@"model:\n    %@", [currentDevice model]]];
  [deviceInfoArray addObject:[NSString stringWithFormat:@"localizedModel:\n    %@", [currentDevice localizedModel]]];
  [deviceInfoArray addObject:[NSString stringWithFormat:@"name:\n    %@", [currentDevice name]]];
  [deviceInfoArray addObject:[NSString stringWithFormat:@"UDID:\n    %@", [currentDevice uniqueIdentifier]]];
  [deviceInfoArray addObject:[NSString stringWithFormat:@"systemName:\n    %@", [currentDevice systemName]]];
  [deviceInfoArray addObject:[NSString stringWithFormat:@"systemVersion:\n    %@", [currentDevice systemVersion]]];
}

- (void)viewDidLoad {
  [super viewDidLoad];

  self.title = @"デバイス情報";
}

- (void)viewWillAppear:(BOOL)animated {
  [self setDeviceInfoArray];
  [super viewWillAppear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  return YES;
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release anything that can be recreated in viewDidLoad or on demand.
	// e.g. self.myOutlet = nil;
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [deviceInfoArray count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
  static NSString *CellIdentifier = @"Cell";
    
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:CellIdentifier] autorelease];
  }
    
	// Configure the cell.
  
  // numberOfLinesが0の場合は、表示行数が無制限となる
  cell.textLabel.numberOfLines = 0;
  cell.textLabel.text = [deviceInfoArray objectAtIndex:indexPath.row];

  return cell;
}

// 表示するセルの高さを返す
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  // 最大表示領域を示すCGSize。このCGSize以上は文字列長がこのサイズを超える場合はすべて表示されない
  CGSize bounds = CGSizeMake(200, 10000);
  // 文字列描画に使用するフォント（正式なアプリの場合は、ここでフォント情報を取得すべき
  UIFont *font = [UIFont systemFontOfSize:14];
  NSString *str = @"";
  if (str == nil) {
    str = @"タイトル";
  } else {
    str = [deviceInfoArray objectAtIndex:indexPath.row];
  }
  // 表示に必要なCGSizeを取得する
  CGSize size = [str sizeWithFont:font 
                constrainedToSize:bounds 
                    lineBreakMode:UILineBreakModeTailTruncation];  
  return size.height + 20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  // セルが選択されたら、内容をアラート表示する
  [self alertWithMessaeg:[deviceInfoArray objectAtIndex:indexPath.row]];
}

#pragma mark -
#pragma mark Alert

/**
 アラート
 */
- (void)alertWithMessaeg:(NSString *)message {
  NSString* title = @"iDC Pro";
  UIAlertView* alert = [[UIAlertView alloc] initWithTitle:title
                                                  message:message 
                                                 delegate:self 
                                        cancelButtonTitle:@"OK" 
                                        otherButtonTitles:nil];
  [alert show];
  [alert release];
}
@end

